import { AudioClip, AudioSource } from "cc";
import Singleton from "../manager/Singleton";
import Utils from './Utils';
import Data from '../manager/Data';

export default class AudioMgr extends Singleton {
    async loadSounds() {
        await Utils.loadBundler("sounds")
    }
    audioSource:AudioSource
    // bgmaudio:AudioClip = null;

    // async playBGM(url: string) {
    //      this.bgmaudio =  (await Utils.loadRes("sounds:"+url,AudioClip)) as AudioClip;// this.getUrl(url);
    //      this.bgmaudio.setLoop(true);
    //      this.bgmaudio.setVolume(Data.user.soundVolume);
    //      this.bgmaudio.play();
    // }

    private lastplaysfxtime = {};
    async playSFX(url: string) {
        if (this.lastplaysfxtime[url]) {
            //if (Date.now() - this.lastplaysfxtime[url] < 300)
           //     return;
        }

        this.lastplaysfxtime[url] = Date.now();
        var ac: AudioClip = (await Utils.loadRes("" + url, "sounds", AudioClip)) as AudioClip;// this.getUrl(url);

        if (Data.user.soundVolume > 0) {
           // audioUrl.setVolume(Data.user.soundVolume);
           // audioUrl.play();
        }

        this.audioSource.playOneShot(ac)
    }

    // setBGMVolume(v: number, force: boolean = false) {
    //     if (Data.user.soundVolume != v || force) {
    //         localStorage.setItem("bgmVolume", v+"");
    //         Data.user.soundVolume = v;
    //         if(this.bgmaudio)
    //         this.bgmaudio.setVolume(Data.user.soundVolume,true);
    //     }
    //     if (v > 0) {
    //         if(this.bgmaudio)
    //         this.bgmaudio.play()
    //     } else {
    //         if(this.bgmaudio)
    //         this.bgmaudio.stop()
    //     }
    // }

    // setSFXVolume(v: number, force: boolean = false) {
    //     if (Data.user.soundVolume != v || force) {
    //         localStorage.setItem("sfxVolume", v+"");
    //         Data.user.soundVolume = v;
    //     }
    // }
}